<template>
  <el-dialog :title="formData.id?'编辑':'新增'" :visible="showDialog" @close="cancelBtn">
    <el-form ref="addDept" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in employeeList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="cancelBtn">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 2.引入编辑接口
import { getDepartments, addDepartments, getDeptDetail, editDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkRepeatName = async(rule, val, callback) => {
      const { depts } = await getDepartments()
      //   判断编辑和新增是否重复
      let isRepeat
      if (this.formData.id) {
        // 编辑
        isRepeat = depts
          .filter(item => item.pid === this.nodeData.pid)
          .some(item => item.name === val && item.id !== this.nodeData.id)
      } else {
        // 新增
        isRepeat = depts
          .filter(item => item.pid === this.nodeData.id)
          .some(item => item.name === val)
      }

      isRepeat ? callback(new Error('同一部门下不能重名')) : callback()
    }
    const checkRepeatCode = async(rule, val, callback) => {
      const { depts } = await getDepartments()
      let isRepeat
      if (this.formData.id) {
        //   编辑
        isRepeat = depts.some(item => item.code === val && item.id === this.nodeData.id)
      } else {
        //   新增
        isRepeat = depts.some(item => item.code === val)
      }

      isRepeat ? callback(new Error('编码必须全公司唯一')) : callback()
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      employeeList: []
    }
  },
  async created() {
    this.employeeList = await getEmployeeSimple()
  },
  methods: {
    async btnOk() {
      await this.$refs.addDept.validate()
      // //  3. 做判断请求发送
      if (this.formData.id) {
        // 编辑
        await editDepartments(this.formData)
      } else {
        // 新增
        await addDepartments({
          ...this.formData,
          pid: this.nodeData.id
        })
      }
      this.$message.success('操作成功')
      this.$emit('addDepartment')
      this.$emit('update:showDialog', false)
    },
    cancelBtn() {
      this.$refs.addDept.resetFields()
      this.$emit('update:showDialog', false)
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async getDeptDetail(id) {
      this.formData = await getDeptDetail(id)
    }
  }
}
</script>

<style>

</style>
